package com.baomidou.wechat.mp.apistore.actuator;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.wechat.mp.apistore.ApiStoreResult;
import com.baomidou.wechat.mp.apistore.vo.WeatherVo;

/**
 * 天气 API 执行 Actuator
 */
public class WeatherActuator extends ApiStoreActuator<WeatherVo> {
	private static String API_WEATHER = "apistore/weatherservice/cityname?cityname=%s";
	private String cityName;

	public static void main(String[] args) {
		ApiStoreActuator<WeatherVo> actuator = new WeatherActuator("北京");
		System.out.println(actuator.execute().toString());
	}

	protected WeatherActuator() {
	}

	public WeatherActuator(String cityName) {
		this.cityName = cityName;
	}

	@Override
	public WeatherVo execute() {
		String json = executeHttpGet(null, API_WEATHER, getCityName());
		if (StringUtils.isNotBlank(json)) {
			ApiStoreResult asr = JSON.parseObject(json, ApiStoreResult.class);
			if (asr != null && asr.isOK()) {
				return JSON.parseObject(asr.getRetData(), WeatherVo.class);
			}
		}
		return null;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

}
